<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/6/2
 * Time: 下午2:36
 */

namespace app\api\controller;


use app\common\controller\ActionLogs;
use app\common\controller\Balance;
use app\common\controller\Bonus;
use app\common\controller\CardPut;
use app\common\controller\ChildCardModify;
use app\common\controller\JobCommon;
use app\common\controller\TemplateMsg;
use app\common\model\CardBonusPut;
use app\common\model\CardConsume;
use app\common\model\CardCzgive;
use app\common\model\CardGeneral;
use app\common\model\CardMember;
use app\common\model\CardUserGet;
use app\common\model\CardUserRecLog;
use app\common\model\CardUserUpdate;
use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\PaymentCardPut;
use think\Queue;
use Wechat\Wxobjopen;
use Wnkj\WnCardObj;

class CardsThree extends Pub
{

    //充送规则列表
    public function czgive_list($param){
        $Merchant = new Merchant();
        $CardMember = new CardMember();
        $CardGeneral = new CardGeneral();
        $maps['id'] = $param['merchantid'];
        $merchantinfo = $Merchant->where($maps)->field('top_mid,id')->find();
        //判断card_id是不是在该商户下
        $maps2['top_mid'] = $merchantinfo['top_mid'];
//        $maps2['card_id'] = $param['card_id'];
//        $maps2['statu'] = 1;
        $maps2['card_type'] = "MEMBER_CARD";
        $qs = $CardMember->where($maps2)->find();
        if ($qs) {
            $maps1['top_mid'] = $merchantinfo['top_mid'];
            $maps1['statu'] = 1;
//            $maps1['card_id'] = $param['card_id'];
//            $page=($param['page']-1)*10;
            $CardCzgive = new CardCzgive();
            $list = $CardCzgive->where($maps1)
//                ->limit($page, 10)
                ->select();
            $total = $CardCzgive->where($maps1)->count();
            if ($list) {
                foreach ($list as $k => $v) {

                    $card_id = $v['zs_card_id_all'];
                    $card_id2 = $v['zs_card_id'];
                    $z1="";
                    $z2="";
                    $resu = '';
                    if ($v['zs_money']) {
                        $z1 = "赠送" . $v['zs_money'] . "元";
                    }

                    if ($v['bonus']) {
                        $z2 = "+" . $v['bonus'] . "积分";
                    }
                    if ($card_id !== '' && $card_id !== null) {
                        $s = json_decode($card_id, true);
                        $ss = "";
                        foreach ($s as $ks => $vs) {
                            if (isset($vs['zs_card_id']) && isset($vs['zs_no'])) {
                                $ss .= $vs['zs_card_id'] . "|" . $vs['zs_no'] . ",";
                            }
                        }

                        $card_id_list = explode(',', rtrim($ss, ','));

                        $res = [];
                        foreach ($card_id_list as $ks => $vs) {
                            $ivs = explode('|', $vs);

                            if (filteroffcard($ivs[0]) == true) {

                                $res[$ks]['title'] = $CardGeneral->where('card_id', $ivs[0])->value('title');
                                $res[$ks]['zs_no'] = $ivs[1];
                            }

                        }
                        if (count($res) >= 1) {
                            $resu = '赠卡券:';
                            if (isset($res)) {
                                foreach ($res as $ks => $vs) {
                                    $resu .= $vs['title'] . "(" . $vs['zs_no'] . "张),";
                                }
                                $resu = rtrim($resu, ',');
                            }
                        }
                    } elseif ($card_id2 !== '' && $card_id2 !== null) {
                        $cardlist2 = explode(',', $card_id2);
                        foreach ($cardlist2 as $ks => $vs) {
                            if (filteroffcard($vs) == true) {

                                $res2[$ks] = $CardGeneral->where('card_id', $vs)->value('title');
                            }
                        }
                        if ($res2) {
                            $resu = '+卡券:';
                            $resu .= implode(',', $res2);
                        }
                    }
                    $list[$k]['name'] = "充值" . $v['cz_money'] . "元" . $z1 . $z2 . $resu;
                }
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'SUCCESS';
                $rarr['result_msg'] = '成功';
                $rarr['total'] = $total;
                $rarr['info'] = $list;
            }else{
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '失败';
            }
        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '会员卡与商户不匹配';
            $rarr['sql'] = $qs;
            $rarr['sqlqq'] = $CardMember->getLastSql();
        }
        return $rarr;
    }


    public function czgive_card_all($param){

        $data = [];
        $param = is_array($param)?$param:json_decode($param,true);

        foreach ($param as $k=>$v){
//            $v = is_array($v)?$v:json_decode($v,true);
            $data[$k]['zs_card_id'] = $v['card_id'];
            $data[$k]['zs_no'] = $v['nums'];
        }
        return $data;
    }

    //创建充送规则
    //
    public function czgive_adds($param)
    {
        $Merchant = new Merchant();
        $CardMember = new CardMember();
        $maps['id'] = $param['merchantid'];
        $merchantinfo = $Merchant->where($maps)->field('top_mid,id')->find();
        //判断card_id是不是在该商户下
        $maps2['top_mid'] = $merchantinfo['top_mid'];
        $maps2['card_type'] = "MEMBER_CARD";
        $card = $CardMember->where($maps2)->field('card_id,top_mid')->find();
        if ($card) {
            $param['type'] = 1;
            $data['bonus'] = $param['bonus'];
            $data['limit_sume'] = $param['limit_sume'];
            $data['top_mid'] = $merchantinfo['top_mid'];
            $data['mid'] = $merchantinfo['id'];
            $data['card_id'] = $card['card_id'];
            $data['cz_money'] = $param['cz_money'];
            $data['type'] = $param['type'];
            $data['zs_money'] = $param['zs_money'];
            $data['zs_show'] = $param['zs_show'];
            if (isset($param['card_arr']) && !empty($param['card_arr'])) {
                $data['zs_card'] = count($param['card_arr']);
                $data['zs_card_id_all'] = json_encode($this->czgive_card_all($param['card_arr']),JSON_UNESCAPED_UNICODE);
            } else {
                $data['zs_card_id_all'] = '';
                $data['zs_card'] = 0;
            }
//            return $data;
            $data['statu'] = $param['statu'];
            $data['create_time'] = time();
            $CardCzgive = new CardCzgive();
            $res = $CardCzgive->allowField(true)->save($data);
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'SUCCESS';
            $rarr['result_msg'] = '创建成功';
            $rarr['info']['cs_id'] = $CardCzgive->getLastInsID();
        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '会员卡与商户不匹配';
        }
        return $rarr;

    }

    //创建充送规则

    public function czgive_edits($param)
    {
        $Merchant = new Merchant();
        $CardMember = new CardMember();
        $maps['id'] = $param['merchantid'];
        $merchantinfo = $Merchant->where($maps)->field('top_mid,id')->find();
        //判断card_id是不是在该商户下
        $maps2['top_mid'] = $merchantinfo['top_mid'];
        $maps2['card_type'] = "MEMBER_CARD";
        $card = $CardMember->where($maps2)->field('card_id,top_mid')->find();
        if ($card) {
            $data['bonus'] = $param['bonus'];
            $data['limit_sume'] = $param['limit_sume'];
            $data['cz_money'] = $param['cz_money'];
            $data['zs_money'] = $param['zs_money'];
            $data['statu'] = $param['statu'];
            $data['zs_show'] = $param['zs_show'];
            if (isset($param['card_arr']) && !empty($param['card_arr'])) {
                $data['zs_card'] = count($param['card_arr']);
                $data['zs_card_id_all'] = json_encode($this->czgive_card_all($param['card_arr']),JSON_UNESCAPED_UNICODE);
            } else {
                $data['zs_card_id_all'] = '';
                $data['zs_card'] = 0;
            }
            $data['update_time'] = time();
            $map['id'] = $param['id'];
            $CardCzgive = new CardCzgive();
            $res = $CardCzgive->allowField(true)->save($data,$map);
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'SUCCESS';
            $rarr['result_msg'] = '修改成功';
        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '会员卡与商户不匹配';
        }
        return $rarr;

    }

    //充送规则状态
    public function czgive_status($param)
    {
        $Merchant = new Merchant();
        $maps['id'] = $param['merchantid'];
        $merchantinfo = $Merchant->where($maps)->field('top_mid,id')->find();
        //判断card_id是不是在该商户下
        $maps2['top_mid'] = $merchantinfo['top_mid'];
//        $maps2['card_id'] = $param['card_id'];
        $maps2['card_type'] = "MEMBER_CARD";
        $qs = CardMember::where($maps2)->count();
        if ($qs) {
            $map['id'] = $param['id'];
            $CardCzgive = new CardCzgive();
            $info = $CardCzgive->where($map)->find();
            if ($info) {
                $data['statu'] = $info['statu'] == 1 ? 0 : 1;
                $res = $CardCzgive->allowField(true)->save($data,$map);
                if ($res) {
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['return_msg'] = 'SUCCESS';
                    $rarr['result_code'] = 'SUCCESS';
                    $rarr['result_msg'] = '设置成功';
                }else{
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['return_msg'] = '';
                    $rarr['result_code'] = 'FAIL';
                    $rarr['result_msg'] = '设置失败';
                }
            }else{
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '此储值异常';
            }
        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '未创建会员卡';
        }
        return $rarr;

    }

    //充送规则状态
    public function czgive_info($param)
    {
        $Merchant = new Merchant();
        $maps['id'] = $param['merchantid'];
        $merchantinfo = $Merchant->where($maps)->field('top_mid,id')->find();
        //判断card_id是不是在该商户下
        $maps2['top_mid'] = $merchantinfo['top_mid'];
//        $maps2['card_id'] = $param['card_id'];
        $maps2['card_type'] = "MEMBER_CARD";
        $qs = CardMember::where($maps2)->count();
        if ($qs) {
            $map['id'] = $param['id'];
            $CardCzgive = new CardCzgive();
            $info = $CardCzgive->where($map)->find()->toArray();
            if ($info) {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = 'SUCCESS';
                $rarr['result_code'] = 'SUCCESS';
                $rarr['result_msg'] = '设置成功';
                $rarr['info'] = $info;
            }else{
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '获取失败';
            }
        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '未创建会员卡';
        }
        return $rarr;
    }


    //支付后送券
    //卡券排列
    public function card_zs_list($data)
    {
        $arr = [];
        foreach ($data['zs_card_id'] as $k => $v) {
            $arr[] = [
                'zs_card_id' => $v,
                'zs_no' => $data['zs_no'][$k],
            ];
        }
        return $arr;
    }

    //
    public function getgivecardnewnoz($card_id, $type)
    {
        $CardGeneral = new CardGeneral();
        if ($type == 0) {
            switch ($type) {
                case "0":
                    $resu = '';
                    if ($card_id !== '' || $card_id !== null) {
                        $s = json_decode($card_id, true);
                        $ss = "";
                        foreach ($s as $k => $v) {
                            $ss .= $v['zs_card_id'] . "|" . $v['zs_no'] . ",";
                        }
                        $card_id_list = explode(',', rtrim($ss, ','));
                        foreach ($card_id_list as $ks => $vs) {
                            $ivs = explode('|', $vs);
                            if ($this->fileoffcards($ivs[0]) == true ) {

                                $res[$ks]['title'] = $CardGeneral->where('card_id', $ivs[0])->value('title');
                                $res[$ks]['zs_no'] = $ivs[1];
                            }
                        }
                        if (!isset($res)) {
                            $res = [];
                        }
                        if (count($res) >= 1) {
                            $resu = '';
                            if (isset($res)) {
                                foreach ($res as $k => $v) {
                                    $resu .= $v['title'] . "(" . $v['zs_no'] . "张),";
                                }
                                $resu = rtrim($resu, ',');
                            }
                        }
                    }
                    return $resu;
                    break;
                case "1":
                    if ($card_id !== '' && $card_id !== null) {
                        $s = json_decode($card_id, true);
                        $ss = "";
                        foreach ($s as $k => $v) {
                            $ss .= $v['zs_card_id'] . ",";
                        }
                        $map['card_id'] = rtrim($ss, ',');
                    }
                    $res = $CardGeneral->where($map)->value('base_info');
                    $res = json_decode($res, true);
                    $res = $res['brand_name'];
                    return $res;
                    break;
            }
        }

    }

    //过滤过期/无库存      可领取列表
    public function fileoffcards($card_id)
    {
        $CardGel = new CardGeneral();
        $card = $CardGel->where('card_id', $card_id)->field('end_timestamp,stocks,consume,time_type')->find();
        if ($card['time_type'] == 'DATE_TYPE_FIX_TIME_RANGE') {
            $time = time();
            if ($card['end_timestamp'] < $time) {
                $res = false;
            } else {
                if ($card['stocks'] > 0) {
                    $res = true;
                } else {
                    $res = false;
                }
            }
        } else {
            $res = true;
        }
        return $res;
    }


    //支付后送券
    public function payment_putcards_list($param){
        try {
            $page=($param['page']-1)*10;
            $Merchant = new Merchant();
            $mach = $Merchant->where(['id'=>$param['merchantid']])->field('top_mid')->find();
            $map['top_mid'] = $mach['top_mid'];
            $PaymentCardPut = new PaymentCardPut();
            $list = $PaymentCardPut->where($map)->limit($page, 10)->select();
            $list = collection($list)->toArray();
            $total = $PaymentCardPut->where($map)->count();
            foreach ($list as $k=>$v){
                $list[$k]['is_member'] = $v['is_member']=='N'?'不可以':'可以';
                $list[$k]['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
                $list[$k]['end_time'] = date('Y-m-d H:i:s',$v['end_time']);
            }
            if ($list) {
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = 'SUCCESS';
                $res_data['result_code'] = 'SUCCESS';
                $res_data['result_msg'] = 'SUCCESS';
                $res_data['total'] = $total;
                $res_data['info'] = $list;
            }else{
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = '';
                $res_data['result_code'] = 'FAIL';
                $res_data['result_msg'] = '未获取到';
            }
        } catch (\Exception $e) {
            $res_data['return_code'] = "FAIL";
            $res_data['return_msg'] = "系统错误";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = $e->getMessage();
        }
        return $res_data;
    }

    //支付后送券 添加
    public function payment_putcards_add($param){
        try {
            $Merchant = new Merchant();
            $PaymentCardPut = new PaymentCardPut();
            $mach = $Merchant->where(['id'=>$param['merchantid']])->field('top_mid')->find();
            //END
            $mab['top_mid'] = $mach['top_mid'];
            $mab['statu'] = 1;
            $info_num = $PaymentCardPut->where($mab)->count();
            if ($info_num == 3) {
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = 'SUCCESS';
                $res_data['result_code'] = 'FAIL';
                $res_data['result_msg'] = '您的已生效规则已达到3条，上架失败！';
            }else{
                //END
                $data['top_mid'] = $mach['top_mid'];
                if ($param['time_type'] == 'lasting') {
                    $data['start_time'] = strtotime(date('Y-m-d h:i:s', time()));
                    $data['end_time'] = strtotime(date('Y-m-d h:i:s', strtotime("+10 year")));
                    $data['times'] = date('Y-m-d h:i:s', $data['start_time']) . '  -  ' . date('Y-m-d h:i:s', $data['end_time']);
                } else {
                    $times = explode(" - ", $param['times']);
//                    return $times;
                    $data['start_time'] = strtotime($times[0]);
                    $data['end_time'] = strtotime($times[1]);
                }
                $cardlist = $this->czgive_card_all($param['cardlist']);
                $data['zs_no'] = count($cardlist);
                $data['zs_card_id_all'] = json_encode($cardlist, JSON_UNESCAPED_UNICODE);
                $data['zs_card_info'] = $this->getgivecardnewnoz($data['zs_card_id_all'], 0);
                $data['cardlist'] = is_array($param['cardlist'])?json_encode($param['cardlist'],true):$param['cardlist'];
                $data['min_fee'] = $param['min_fee'];
                $data['max_fee'] = $param['max_fee'];
                $data['is_member'] = $param['is_member'];
                $data['statu'] = 1;
                $data['create_time'] = time();
                $res = $PaymentCardPut->allowField(true)->save($data);
                if ($res) {
                    $res_data['return_code'] = "SUCCESS";
                    $res_data['return_msg'] = 'SUCCESS';
                    $res_data['result_code'] = 'SUCCESS';
                    $res_data['result_msg'] = 'SUCCESS';
                } else {
                    $res_data['return_code'] = "SUCCESS";
                    $res_data['return_msg'] = '';
                    $res_data['result_code'] = 'FAIL';
                    $res_data['result_msg'] = '未获取到';
                }
            }
        } catch (\Exception $e) {
            $res_data['return_code'] = "FAIL";
            $res_data['return_msg'] = "系统错误";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = $e->getMessage();
        }
        return $res_data;
    }


    //支付后送券删除
    public function payment_putcards_del($param)
    {
        try {
            $Merchant = new Merchant();
            $mach = $Merchant->where(['id'=>$param['merchantid']])->field('top_mid')->find();
            $map['top_mid'] = $mach['top_mid'];
            $map['id'] = $param['id'];
            $PaymentCardPut = new PaymentCardPut();
            $res = $PaymentCardPut->where($map)->delete();
            if ($res) {
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = 'SUCCESS';
                $res_data['result_code'] = 'SUCCESS';
                $res_data['result_msg'] = '删除成功';
            }else{
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = '';
                $res_data['result_code'] = 'FAIL';
                $res_data['result_msg'] = '删除失败';
            }
        } catch (\Exception $e) {
            $res_data['return_code'] = "FAIL";
            $res_data['return_msg'] = "系统错误";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = $e->getMessage();
        }
        return $res_data;
    }


    //支付后送券状态
    public function payment_putcards_statu($param)
    {

        try {
            $Merchant = new Merchant();
            $mach = $Merchant->where(['id'=>$param['merchantid']])->field('top_mid')->find();
            $map['top_mid'] = $mach['top_mid'];
            $map['id'] = $param['id'];
            $PaymentCardPut = new PaymentCardPut();
            $info = $PaymentCardPut->where($map)->find();
            //END
            $mab['top_mid'] = $mach['top_mid'];
            $mab['statu'] = 1;
            $info_num = $PaymentCardPut->where($mab)->count();
            if ($info) {
                $data['statu'] = $info['statu'] == 1 ? 0 : 1;
                if ($info_num == 3 && $data['statu'] == 1) {
                    $res_data['return_code'] = "SUCCESS";
                    $res_data['return_msg'] = 'SUCCESS';
                    $res_data['result_code'] = 'FAIL';
                    $res_data['result_msg'] = '您的已生效规则已达到3条，上架失败！';
                }else {
                    $res = $PaymentCardPut->save($data, $map);
                    if ($res) {
                        $res_data['return_code'] = "SUCCESS";
                        $res_data['return_msg'] = 'SUCCESS';
                        $res_data['result_code'] = 'SUCCESS';
                        $res_data['result_msg'] = '上架成功';
                    } else {
                        $res_data['return_code'] = "SUCCESS";
                        $res_data['return_msg'] = 'SUCCESS';
                        $res_data['result_code'] = 'FAIL';
                        $res_data['result_msg'] = '上架失败';
                    }
                }
            }else{
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = '';
                $res_data['result_code'] = 'FAIL';
                $res_data['result_msg'] = '状态异常';
            }
        } catch (\Exception $e) {
            $res_data['return_code'] = "FAIL";
            $res_data['return_msg'] = "系统错误";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = $e->getMessage();
        }
        return $res_data;
    }

    //支付后送券非会员
    public function payment_putnotuser_statu($param)
    {

        try {
            $Merchant = new Merchant();
            $mach = $Merchant->where(['id'=>$param['merchantid']])->field('top_mid')->find();
            $map['top_mid'] = $mach['top_mid'];
            $map['id'] = $param['id'];
            $PaymentCardPut = new PaymentCardPut();
            $info = $PaymentCardPut->where($map)->find();
            if ($info) {
                $data['is_member'] = $info['is_member'] == 'Y' ? 'N' : 'Y';

                    $res = $PaymentCardPut->where($map)->update($data);
                    if ($res) {
                        $res_data['return_code'] = "SUCCESS";
                        $res_data['return_msg'] = 'SUCCESS';
                        $res_data['result_code'] = 'SUCCESS';
                        $res_data['result_msg'] = '上架成功';
                    } else {
                        $res_data['return_code'] = "SUCCESS";
                        $res_data['return_msg'] = 'SUCCESS';
                        $res_data['result_code'] = 'FAIL';
                        $res_data['result_msg'] = '上架失败';
                    }

            }else{
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = '';
                $res_data['result_code'] = 'FAIL';
                $res_data['result_msg'] = '状态异常';
            }
        } catch (\Exception $e) {
            $res_data['return_code'] = "FAIL";
            $res_data['return_msg'] = "系统错误";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = $e->getMessage();
        }
        return $res_data;
    }


    //群发卡券
    public function send_user_bonusmsg($param){
        $Merchant = new Merchant();
        $CardMember = new CardMember();
        $CardUserGet = new CardUserGet();
        $CardBonusPut = new CardBonusPut();
        $mach = $Merchant->where(['id'=>$param['merchantid']])->field('top_mid,id')->find();
        $card = $CardMember->where(['top_mid'=>$mach['top_mid']])->field('id,card_id')->find();

        if ($mach && $card) {
            $sdata['param']['card_id'] = $card['card_id'];
            $sdata['param']['add_bonus'] = $param['add_bonus_mass'];
            $sdata['param']['remark'] = $param['remark_mass'];
            $sdata['param']['action_user'] =$param['device_id'];

            $map['top_mid'] = $mach['top_mid'];
            $map['id'] = ['in',[4457,267993]];
//            $map['id'] = array('in', $param['senduserlist_mass']);
            $total = $CardUserGet->where($map)->count();

            $ress = ['error'];
            $JobCommon = new JobCommon();
            for ($i = 0; $i < $total; $i = $i + 50) {
                $sdata['param']['userlist'] = $CardUserGet->where($map)->field('code,card_id')->limit($i, 50)->select();
//                $ress[] = $JobCommon->task_bonus_put($sdata);
                $ress[$i] = $this->task_bonus_put($sdata);
            }
            $data['res_info'] = json_encode($ress, JSON_UNESCAPED_UNICODE);
            $data['top_mid'] = $mach['top_mid'];
            $data['card_id'] = $card['card_id'];
            $data['remark'] = $param['remark_mass'];
            $data['add_bonus'] = $param['add_bonus_mass'];
//            $data['senduserlist'] = $param['senduserlist_mass'];
            $data['create_time'] = time();
            $res = $CardBonusPut->save($data);
            if ($res) {
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "SUCCESS";
                $res_data['result_msg'] = "发送成功";
            } else {
                $res_data['return_code'] = "SUCCESS";
                $res_data['return_msg'] = "SUCCESS";
                $res_data['result_code'] = "FAIL";
                $res_data['result_msg'] = "发送失败";
            }
        }else{
            $res_data['return_code'] = "SUCCESS";
            $res_data['return_msg'] = "SUCCESS";
            $res_data['result_code'] = "FAIL";
            $res_data['result_msg'] = "功能暂不可用";
        }
        return $res_data;
    }

    //批量执行积分变动(积分
    public function task_bonus_put($param)
    {
        //初始化微信类
        if (isset($param['param']['card_id'])) {
            $userlist = $param['param']['userlist'];                                    //用户列表
            foreach ($userlist as $k => $v) {
                $option['card_id'] = $param['param']['card_id'];                        //card_id
                $option['code'] = $v['code'];                                           //code
                $option['orderid'] = '';
                $option['num'] = $param['param']['add_bonus'];                          //add_bonus
                $option['bd_cate'] = 'bonus';
                $option['type'] = 1;//变动 1增加 2减少
                $option['cate'] = 1;  //变动类型
                $option['pay_cate'] = 4; //支付方式
                $option['source'] = 'web';  //来源
                $option['remark'] = $param['param']['remark']; //备注                   //remark
                $option['action_user'] = $param['param']['action_user'];//操作人        //action_user
                $Fztest = new WnCardObj($option);
                $res = $Fztest->init();
                $datass[$k]['info'] = json_encode($res, JSON_UNESCAPED_UNICODE);

            }

            return 'success';
        }else{
            return 'error';
        }

//        if (isset($param['param']['card_id'])) {
//            $userlist = $param['param']['userlist'];                                    //用户列表
//            foreach ($userlist as $k => $v) {
//                $option['card_id'] = $param['param']['card_id'];                        //card_id
//                $option['code'] = $v['code'];                                           //code
//                $option['orderid'] = '';
//                $option['num'] = $param['param']['add_bonus'];                          //add_bonus
//                $option['bd_cate'] = 'bonus';
//                $option['type'] = 1;//变动 1增加 2减少
//                $option['cate'] = 1;  //变动类型
//                $option['pay_cate'] = 4; //支付方式
//                $option['source'] = 'web';  //来源
//                $option['remark'] = $param['param']['remark']; //备注                   //remark
//                $option['action_user'] = $param['param']['action_user'];//操作人        //action_user
//                $Fztest = new WnCardObj($option);
//                $res = $Fztest->init();
//                $datass[$k]['info'] = json_encode($res,JSON_UNESCAPED_UNICODE);
//            }
//
//            return 'success';
//        }
    }



    public function getsignkey($appid)
    {

        $Agent = new \app\common\model\Agent();
        $signkey = $Agent->where(array('sign_appid' => $appid))->value('sign_key');
        return $signkey;
    }

    public function uni_array($arr)
    {
        foreach ($arr as $k => $v) {
            if (!$v)
                unset($arr[$k]);
        }
        return $arr;
    }

    public function getsign($arrdata, $method = "sha1", $appid = "")
    {
        try {
            if (!function_exists($method)) return false;
            ksort($arrdata);
            $paramstring = "";
            foreach ($arrdata as $key => $value) {
                if (isset($value) && !empty($value) or $value=="0" or $value=="") {
                    if (is_array($value)) {
                        $value = json_encode($value, JSON_UNESCAPED_UNICODE);
                    }
                    $str[] = $key . "=" . $value;
                }
            }
            $paramstring = join("&", $str);
            if (!empty($appid)) {
                $signkey = getsignkey($appid);
            } else {
                $signkey = getsignkey($arrdata['appid']);
            }
            $Sign = $method($paramstring . "&key=" . $signkey);

            return $Sign;
        } catch (\Exception $e) {
            return $e;
        }
    }


}